package com.fuze.energy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fuze.energy.common.Advice;
import com.fuze.energy.common.CustomException;
import com.fuze.energy.domain.dto.UserDTO;
import com.fuze.energy.domain.entity.User;
import com.fuze.energy.mapper.UserMapper;
import com.fuze.energy.service.UserService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author sepc_
 * @date 7/29/2021 16:02
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    final UserMapper userMapper;

    @Override
    public User doLogin(UserDTO userDTO) {
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        User user = userMapper.selectOne(qw
                .select()
                .eq(StringUtils.isNotBlank(userDTO.getUsername()), User::getUsername, userDTO.getUsername()
                ));
        if (user == null) {
            throw new CustomException(Advice.USER_NOT_EXIST);
        }
        if (!user.getPassword().equals(userDTO.getPassword())) {
            throw new CustomException(Advice.WRONG_PASSWORD);
        }
        user.setId(null);
        user.setPassword(null);
        return user;
    }
}
